package com.jz.cash.client.shiro;

import com.alibaba.fastjson.JSON;
import com.jz.cash.client.model.User;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CredentialMatcher extends SimpleCredentialsMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        User userDB =  (User) info.getPrincipals().asList().get(0);


        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;

        String pwd =new SimpleHash(Sha256Hash.ALGORITHM_NAME, usernamePasswordToken.getPassword(),userDB.getSalt() , 1024).toString();
        return this.equals(pwd, info.getCredentials().toString());
    }
}
